package net.lainiao.mycost.utils;


import net.lainiao.mycost.bean.UserInfo;
import org.activiti.engine.impl.cmd.DeleteUserInfoCmd;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * Created by Administrator on 2017/3/21.
 */
public class LoginFilter implements Filter {
    public LoginFilter() {
        System.out.println("LoginFilter Created");
    }

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain chain) throws IOException, ServletException {
        HttpServletResponse response=(HttpServletResponse) servletResponse;
        HttpServletRequest request=(HttpServletRequest) servletRequest;
        HttpSession session=request.getSession();
        UserInfo userInfo=(UserInfo)session.getAttribute("user");
        String loginJsp=request.getContextPath()+"/login_info.jsp";
        String url=request.getRequestURL().toString();
        if(userInfo!=null){
            chain.doFilter(request,response);
        }else if(url.contains("/login.jsp")){
            chain.doFilter(request,response);
        }else if(url.contains("/login_info.jsp")){
            chain.doFilter(request,response);
        }else if(url.contains("/res/")){
            chain.doFilter(request,response);
        }else if(url.contains("login.do")){
            chain.doFilter(request,response);
        }else{
            response.sendRedirect(loginJsp);
        }
    }

    @Override
    public void destroy() {

    }
}
